   unit YearClassNPlot;

interface


uses DiagramFactory, datacontainer, pganalysistypesunit, apgabstractunit, classes,
   xmlintf, pgseriesunit, chartaxisunit, ifmmemoryunit, math, sysutils, apgabstracttypesunit;

type

   YearClassNDiagram = class(TasacsDiagram)
   private
      m_HideNValuesWithZeroWeight : Boolean;
   protected
      function GetFigPropName(Prop : PgFigProp) : string; override;
      function GetFigPropSet : PgFigPropSet; override;
      function IsPropChecked(Prop : PgFigProp) : Boolean; override;
      procedure CheckProp(Prop : PgFigProp; Value : Boolean); override;
   public
      property HideNValuesWithZeroWeight : Boolean read m_HideNValuesWithZeroWeight write m_HideNValuesWithZeroWeight;
   end;  // YearClassNDiagram


   YearClassNChart = class(TasacsChart)
   private
      m_HideNValuesWithZeroWeight : Boolean;
      m_Cohort, m_iDgr : Integer;
   public
      function CreateChartAxis(Axis : PgAxisType) : APgChartAxis; override;
      function CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries; override;
      constructor Create(AOwner : TComponent; datacontainer : TasacsDataContainer; iCohort, iDgr : Integer); reintroduce;
      property Cohort : Integer read m_Cohort;
      property Dgr : Integer read m_iDgr;
   end; // YearClassNChart


   YearClassNSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
      function GetShowZeros() : Boolean; override;
   end; // YearClassNSeries


   YearClassNAgeAxis = class(DsYearAxis)
   protected
      function GetNumDimensionElms : Integer; override;
   end; // YearClassNAgeAxis


   YearClassNAgeAxisSeries = class(AgeAxisSeries)
   protected
      function GetIntervalName(I : Integer) : string; override;
   end; // YearClassNAgeAxisSeries


implementation


const fpHideNValuesWithZeroWeight = fpUser1;

//////////////////////
// YearClassNDiagram

function YearClassNDiagram.GetFigPropName(Prop : PgFigProp) : string;
begin
   case Prop of
      fpHideNValuesWithZeroWeight: Result := 'Hide N values with zero weight';
   else
      Result := inherited GetFigPropName(Prop);
   end; // end;
end; // YearClassNDiagram.GetFigPropName


function YearClassNDiagram.GetFigPropSet : PgFigPropSet;
(* Purpose:  Returns the figure property set from figure property set type.
   In:       FigPropSetType : Figure property set type
   History:  When     Who         What
             040817   Skalevik    Commented
*)
begin
   Result := inherited GetFigPropSet + [fpHideNValuesWithZeroWeight];
end; // YearClassNDiagram.GetFigPropSet


function YearClassNDiagram.IsPropChecked(Prop : PgFigProp) : Boolean;
begin
   case Prop of
      fpHideNValuesWithZeroWeight: Result := HideNValuesWithZeroWeight;
   else
      Result := inherited IsPropChecked(Prop);
   end; // end;
end; // YearClassNDiagram.IsPropChecked


procedure YearClassNDiagram.CheckProp(Prop : PgFigProp; Value : Boolean);
begin
   case Prop of
      fpHideNValuesWithZeroWeight: HideNValuesWithZeroWeight := Value;
   else
      inherited CheckProp(Prop, Value);
   end;
   OnDiagramPropChanged;
end; // YearClassNDiagram.CheckProp


//////////////////////////////////
// YearClassNChart


function YearClassNChart.CreateChartAxis(Axis : PgAxisType) : APgChartAxis;
begin
   case Axis of
      atx:
         Result := YearClassNAgeAxis.Create(Self as IChart, Axis);
   else
      Result := PgChartAxis.Create(Self as IChart, Axis);
   end; // case
end; // YearClassNChart.CreateChartAxis


function YearClassNChart.CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries;
begin
   Result := nil;
   case PrimaryChartAxis.axis of
      atX:
         if Assigned(MetaSeries) then
           Result := YearClassNSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex)
         else
            Result := YearClassNAgeAxisSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex);
   end; // case
end; // YearClassNChart.CreateSeries


constructor YearClassNChart.Create(AOwner : TComponent; datacontainer : TasacsDataContainer; iCohort, iDgr : Integer);
var
   M : APgMetaSeries;
   NumSeries : Integer;
   SeriesName : string;
   I, iDs : Integer;
   dsVar : DataSourceVar;
   ShowSeries : Boolean;
begin
   m_Cohort := iCohort;
   m_iDgr := iDgr;
   inherited Create(AOwner, nil, '', datacontainer);
   Caption := 'Cohort ' + inttostr(datacontainer.BirthYearFromCohort[iCohort]);

   ChartLegend := Caption;
   ShowChartLegend := true;
   ShowSeriesLegend := (Cohort mod 9) = 8;
   ShowMeanLegend := false;
//      ChartAxis[atX].ShowIntervalSeries := true;
   PlotType := ptCategorySingle;
   chartaxis[atX].ContinuousGroupData := false;
   chartaxis[atX].ShowMinorTick := true;
   chartaxis[atX].ShowMajorGrid := false;
//   chartaxis[aty].DefaultMin := true;
   chartaxis[aty].ShowLabel := false;
   chartaxis[atX].ShowLabel := false;
   //chartaxis[atX].AxisLabelPosition := alpCenter;
//   chartaxis[aty].AxisLabel := 'Age';
   chartaxis[aty].AxisLabelPosition := alpCenter;
   chartaxis[atY].ShowMajorGrid := false;
   chartaxis[aty].DefaultAxisLabel := false;
   chartaxis[aty].LnTransformed := true;
   chartaxis[aty].DefaultMin := true;
   chartaxis[aty].DefaultMax := true;
   // Setup meta series at y axis:
   NumSeries := 1 + datacontainer.ds.count;
   with ChartAxis[atY].MetaSeries do
   begin
      for I := 0 to NumSeries - 1 do
      begin
         case I of
            0:
               SeriesName := 'N from assessment';
         else
            SeriesName := 'N from ' + datasourcecontainer(datacontainer.ds[I - 1]).Key;
         end;
         ShowSeries := true;
         M := TasacsMetaSeries.Create(ChartAxis[aty], nil, SeriesName, I + 1);
         M.ShowSeries := ShowSeries;
         Add(Pointer(M));
         M.BarPlot := false;
         (M as IPgChartSelection).GraphLayout.LineWidth[geMarkerLine] := 2;
         M.Scatter := true;
         M.LinePlot := true;
         //{ --- trend view---
         M.SuperTrend := false;
         M.Trend.TrendFunctionType := ftMovAvg;
         //}
      end;
   end;
end; // YearClassNChart.Create


///////////////////////////////
// YearClassSeries


function YearClassNSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
var
   S : Integer;
   iYear, iAge, iObsYear, iObsAge, iCohort, iDs : Integer;
   dsc : DataSourceContainer;
   dsVar : DataSourceVar;
   ds, dsw : FloatMatrix;
   TargetDgr : YearClassNDiagram;
begin
   case SeriesVariable of
      svIntervalDelta:
         with YearClassNChart(PrimaryChartAxis.ChartComp) do
         begin
            iCohort := Cohort;
            iYear := datacontainer.FirstYearIndexInCohort[iCohort] + (I + MinCategory);
            iAge := datacontainer.FirstAgeIndexInCohort[iCohort] + (I + MinCategory);
            S := Self.MetaSeries.ChartAxis.MetaSeries.IndexOf(Pointer(Self.MetaSeries));
            case S of
               0:
                  Result := FloatMatrix(datacontainer.StdData[ord(stdN)])[iYear, iAge]
            else
               dsc := datasourcecontainer(datacontainer.ds[S - 1]);
               iObsYear := iYear + datacontainer.FirstYear - dsc.FirstYear;
               iObsAge := iAge + datacontainer.FirstAge - dsc.FirstAge;
               ds := FloatMatrix(dsc.dsdata[ord(dsNFromDS)]);
               dsw := FloatMatrix(dsc.dsdata[ord(dsWeight)]);
               if (0 <= iObsYear) and (iObsYear <= ds.GetSize(1) - 1) and
                  (0 <= iObsAge) and (iObsAge <= ds.GetSize(2) - 1) then
               begin
                  Result := ds[iObsYear, iObsAge];
                  TargetDgr := YearClassNDiagram((PrimaryChartAxis.ChartComp.Owner as ITasacsDiagramParent).GetYearClassNDiagram(Dgr));
                  if TargetDgr.HideNValuesWithZeroWeight and (dsw[iObsYear, iObsAge] <= 0) then
                     Result := -1;
               end
               else
                  Result := -1;

            end;
            if Result = -1 then
               Result := nan
         end;
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries)
   end; // case
end; // YearClassNSeries.GetInternalValue


function YearClassNSeries.GetShowZeros() : Boolean;
begin
   Result := false;
end;


//////////////////////////////////
// YearClassNAgeAxis


function YearClassNAgeAxis.GetNumDimensionElms : Integer;
begin
   with YearClassNChart(ChartComp) do
      Result := DataContainer.NumElmsInCohort[Cohort];
end; // YearClassNAgeAxis.GetNumDimensionElms


function YearClassNAgeAxisSeries.GetIntervalName(I : Integer) : string;
var
   Age : Integer;
begin
   // I range : [0..NumDimElms - 1]; // not relative to axis selection.
   with YearClassNChart(PrimaryChartAxis.ChartComp) do
      Age := I + MinCategory + DataContainer.FirstAge + DataContainer.FirstAgeIndexInCohort[Cohort];;
   Result := inttostr(Age);
end; // DsAgeAxisSeries.GetIntervalName

end.
